#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "nussinov.h"

void init_array(int n, char seq[60], int table[60][60]) {
    int i, j;

    for (i = 0; i < n; i++) {
        seq[i] = (char)((i + 1) % 4);
    }

    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            table[i][j] = 0;
}

void print_array(int n, int table[60][60])
{
    int i, j;
    int t = 0;

    fprintf(stderr, "==BEGIN DUMP_ARRAYS==\n");
    fprintf(stderr, "begin dump: %s", "table");
    for (i = 0; i < n; i++) {
        for (j = i; j < n; j++) {
            if (t % 20 == 0)
                fprintf(stderr, "\n");
            fprintf(stderr, "%d ", table[i][j]);
            t++;
        }
    }
    fprintf(stderr, "\nend   dump: %s\n", "table");
    fprintf(stderr, "==END   DUMP_ARRAYS==\n");
}

int main() {
    int n = 60;
    int has_errors = 0;

    char seq[60];
    int table[60][60];
    int check_table;

    init_array(n, seq, table);

    kernel_nussinov(seq, table);

    FILE *tb_file = fopen("check.data", "r");
    assert(tb_file != NULL && "Couldn't open check data file");

    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            int res = fscanf(tb_file, "%d", &check_table);
            assert(res == 1 && "read check data file failed");
            if (table[i][j] != check_table) {
                printf("table[%d][%d] = %d, expected_table[%d][%d] = %d\n",
                       i, j,
                       table[i][j],
                       i, j,
                       check_table);
                has_errors++;
            }
        }
    }

    fclose(tb_file);

    if (has_errors) {
        fprintf(stderr, "Benchmark results are incorrect\n");
        return -1;
    }

    printf("Success.\n");
    return 0;
}
